package com.eduhzy.ttw.work.mvp.presenter;

import android.app.Application;
import android.os.Message;

import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.work.mvp.contract.CheckWorkContract;
import com.eduhzy.ttw.work.mvp.model.entity.WorkCheckDetailData;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import org.simple.eventbus.EventBus;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


/**
 * @author xubo
 * @date 2018-12-3 17:45:21
 */
@ActivityScope
public class CheckWorkPresenter extends BasePresenter<CheckWorkContract.Model, CheckWorkContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public CheckWorkPresenter(CheckWorkContract.Model model, CheckWorkContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void workRecordDetail4Teacher(int workId, int classId, int studentId) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("workId", workId);
        map.put("classId", classId);
        map.put("studentId", studentId);
        map.put("userId", SPUtils.getInstance().getString(Constants.USERID));

        mModel.workRecordDetail4Teacher(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    mRootView.hideLoading();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<WorkCheckDetailData>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<WorkCheckDetailData> commonData) {
                        if (commonData.isResult()) {
                            mRootView.update(commonData.getData());
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });
    }

    public void checkWorkRecord(int workId, int classId, int studentId, String comment, int score, int type) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("userId", SPUtils.getInstance().getString(Constants.USERID));
        map.put("workId", workId);
        map.put("classId", classId);
        map.put("studentId", studentId);
        map.put("comment", comment);
        map.put("score", score);
        map.put("type", type);
        mModel.checkWorkRecord(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    mRootView.hideLoading();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<WorkCheckDetailData>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<WorkCheckDetailData> commonData) {
                        if (commonData.isResult()) {
                            if (commonData.getCount() <= 0) {
                                mRootView.showMessage(commonData.getMessage());
                                Message msg = Message.obtain();
                                msg.what = EventBusTags.CHECK_COMPLETE;
                                EventBus.getDefault().post(msg,RouterHub.WORK_WORKSTATUSFRAGMENT);
                                EventBus.getDefault().post(msg,RouterHub.WORK_WORKUNSUBMITFRAGMENT);
                                mRootView.killMyself();
                                return;
                            }
                            mRootView.update(commonData.getData());
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });
    }
}
